package es.ats.codington.festival.services;

import java.util.ArrayList;
import java.util.List;

import es.ats.codington.festival.daos.ManagerDAO;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.interfaces.services.IEventSignUpService;

/**
 * 
 * @author Codington_RED
 *
 */
public class EventSignUpService implements IEventSignUpService{

	/**
	 * A method service that check the events which a visitor are registered in 
	 * @param eventSignUp
	 * @return isCreated
	 * @throws ServiceException
	 */
	@Override
	public boolean checkEventsofVisitor(EventSignUp eventSignUp)
			throws ServiceException {
				
		boolean isCreated = false;
		ManagerDAO manager = null;
		IEventSignUpDAO eventSignUpDAO = null;
		
		try {
			manager = new ManagerDAO();
			eventSignUpDAO = manager.getEventSignUpDAO();
			isCreated = eventSignUpDAO.checkEventsofVisitor(eventSignUp);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return isCreated;
	}

	
	/**
	 * method to unregister of any Event.
	 * @param eventSignUp
	 * @return isCreated
	 * @throws ServiceException
	 */
	@Override
	public boolean unregisterVisitorToEvent(EventSignUp eventSignUp) throws ServiceException{
		boolean isCreated = false;
		ManagerDAO manager = null;
		IEventSignUpDAO eventSignUpDAO = null;
		
		try {
			manager = new ManagerDAO();
			eventSignUpDAO = manager.getEventSignUpDAO();
			isCreated = eventSignUpDAO.unregisterVisitorToEvent(eventSignUp);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		return isCreated;
		
	}

	
	/**
	 * method to register in a event
	 * @param eventsignup
	 * @return isCreated
	 * @throws ServiceException
	 */
	@Override
	public boolean registerVisitorToEvent(EventSignUp eventsignup)
			throws ServiceException {
		
		boolean isCreated = false;
		ManagerDAO manager = null;
		IEventSignUpDAO eventSignUpDAO = null;
		
		try {
			manager = new ManagerDAO();
			eventSignUpDAO = manager.getEventSignUpDAO();
			isCreated = eventSignUpDAO.registerVisitorToEvent(eventsignup);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return isCreated;
	}

	
	/**
	 * method that show us a list with all event which the visitor are registered in
	 * @param eventSignUp
	 * @return events
	 * @throws ServiceException
	 */
	@Override
	public List<Event> registeredEvents(EventSignUp eventSignUp)
			throws ServiceException {
		
		ManagerDAO manager = null;
		//EventSignUp eventSignUp = new EventSignUp();
		IEventSignUpDAO eventSignUpDAO = null;
		List<Event> events = new ArrayList<Event>();	
	
		try {
		    manager = new ManagerDAO();
		    eventSignUpDAO = manager.getEventSignUpDAO();
		    	     
		    //eventSignUp.setIdVisitor(visitor.getVisitorId());
		    events = eventSignUpDAO.registeredEvents(eventSignUp);
	
		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}
	
		return events;
	}

}
